package com.github.llyb120.qiuya.storage.bptree;

import com.github.llyb120.qiuya.storage.VirtualFile;

import static com.github.llyb120.qiuya.util.Convert.byte2long;
import static com.github.llyb120.qiuya.util.Convert.longToByteArray;

public final class Block {
    byte[] bytes;
    long offset;
    //是否被使用
//    public boolean used = false;
    //关联的节点
//    public BPlusTreeNode node;

    //缓存索引,可以不用
//    public int index = -1;

    int blockSize;

    private Block() {
    }

    public static Block getBlock(VirtualFile file, long offset, int blockSize){
        Block block = new Block();
        block.offset = offset;
        block.blockSize = blockSize;
        block.bytes = file.readBytes(offset, blockSize);
        return block;
    }

    public long readLong(int offset){
        return byte2long(bytes, offset);
    }

//    public void readFromFile(VirtualFile file, long position){
//        file.readBytes(position, bytes, 0, blockSize);
//        int offset = 0;
////        node.self = byteArrayToLong(bytes,offset);
////        offset += Long.BYTES;
//        node.parent = byteArrayToLong(bytes,offset);
//        offset += Long.BYTES;
////        node.prev = byteArrayToLong(bytes,offset);
////        offset += Long.BYTES;
//        node.next = byteArrayToLong(bytes,offset);
//        offset += Long.BYTES;
//    }
//
//    public void writeToFile(VirtualFile file){
//        int offset = 0;
////        longToByteArray(node.self, bytes, offset);
////        offset += Long.BYTES;
//        longToByteArray(node.parent, bytes, offset);
//        offset += Long.BYTES;
////        longToByteArray(node.prev, bytes, offset);
////        offset += Long.BYTES;
////        longToByteArray(node.next, bytes, offset);
////        file.writeBytes(node.self, bytes);
//    }
}
